package com.briup.web;

import com.briup.pojo.Customer;
import com.briup.pojo.Order;
import com.briup.pojo.Shopcar;
import com.briup.service.IOrderService;
import com.briup.service.IShopCarService;
import com.briup.serviceImp.OrderServiceImpl;
import com.briup.serviceImp.ShopCarServiceImp;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.List;


public class OrderServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		HttpSession session = request.getSession();
		List<Shopcar> shopcar = (List<Shopcar>) session.getAttribute("shopcar");
		Customer customer = (Customer) session.getAttribute("login");
		//总价钱
		float total =  Float.parseFloat(request.getParameter("total"));
		
		//生成订单、订单项，清空购物车
		IOrderService service = new OrderServiceImpl();
		service.saveOrder(new Order(total, new Timestamp(System.currentTimeMillis()), customer.getId()), shopcar);

		//更新购物车中的信息
		IShopCarService scservice = new ShopCarServiceImp();
		List<Shopcar> cars = scservice.findByCustomer(customer);
		session.setAttribute("shopcar", cars);
		//更新session中的订单
		List<Order> orders = service.findByCustomer(customer);
		session.setAttribute("orders", orders);
		
		session.setAttribute("total", total);
		response.sendRedirect(request.getContextPath() + "/confirmSuc.jsp");
	}
}
